import java.util.Collections;

// Pipeline - Mapping
public class Mapping {
  ArrayList<Sniffer> mappedSniffers;   // = new ArrayList<Sniffer>(); // use refence
  ArrayList<Node> mappedNodes;         // pass by Refence

  float min_long;
  float max_long;
  float min_lat;
  float max_lat;
  float min_alt; // Height, min = like basement will be below floor level
  float max_alt;
  
  float[] geoLongList;
  float[] geoLatList;
  float[] geoAltList;
  
  float PI = 3.14159265;

  /**
   *  This class use it to Map Lat, long To our world, 
   *  and does not draw anything to the screen (not directly anyways)
   */

  Mapping(ArrayList<Sniffer> geoSniffers, ArrayList<Node> geoNodes) {
    this.mappedSniffers = geoSniffers;
    this.mappedNodes = geoNodes;
    // lat , long position processing
    this.geoLongList = new float[geoSniffers.size()];
    this.geoLatList  = new float[geoSniffers.size()];
    this.geoAltList  = new float[geoSniffers.size()];
    
    // Set position 
    for (int i = 0; i < geoSniffers.size(); i++) {
      Sniffer each = geoSniffers.get(i);
      
      geoLongList[i] = each.getGeoLong();
      geoLatList[i] = each.getGeoLat();
      geoAltList[i] = each.getGeoAlt();
    }
    
    // Get the max and min location of the sniffers
    this.min_long = min(this.geoLongList);
    this.max_long = max(this.geoLongList);
    this.min_lat  = min(this.geoLatList);
    this.max_lat  = max(this.geoLatList);
    this.min_alt  = min(this.geoAltList);
    this.max_alt  = max(this.geoAltList);
    for (int i = 0; i < this.mappedSniffers.size(); i++) {
      Sniffer each   = this.mappedSniffers.get(i);
      // LONG LAt to Screen
      each.setLocationX(map(each.getGeoLong(), this.min_long, this.max_long, -width/2, width/2));
      each.setLocationY(map(each.getGeoLat(), this.min_lat, this.max_lat, -height/2, height/2));
      if (this.min_alt != this.max_alt){
        each.setLocationZ(map(each.getGeoAlt(), this.min_alt, this.max_alt, 0, FLOOR_HEIGHT*random(FLOOR_NUM)));
      }else{
        each.setLocationZ(0);
      }
    }
  }
  void draw(){
    // set position for nodes
    for (Sniffer sniffer : this.mappedSniffers){
      String s_hostname = sniffer.getHostname();
      sniffer.nodeClear();
      for (Node node : this.mappedNodes){
        if (node.getnewloc() == 1){
          if (s_hostname.equals(node.getHostname())){
            node.setnewloc(0);
            sniffer.nodeInc();
            node.setHost(sniffer);
            // setHost also will set x,y location
          }else{
            sniffer.nodeDec();
          }
        }
      }
    }
  }
  ArrayList <Sniffer> getSniffers() {
    //converted locations in relation to screen screen size
    return this.mappedSniffers;
  }
  
  ArrayList <Node> getNodes() {
    //converted locations in relation to screen screen size
    return this.mappedNodes;
  }
}
